package acm.蓝桥17;

import java.io.*;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;

public class D岛的的个数 {
    /**
     * 洪水填充
     */
    static int maxN = 50;
    static int[][] maze = new int[maxN + 1][maxN+1];
    static boolean[][] ring;
    static int n, m;

    public static void main(String[] args) {
        Reader sc = new Reader();
        int t = sc.nextInt();
        for (int i = 0; i < t; i++) {
            n = sc.nextInt();
            m = sc.nextInt();
            ring = new boolean[n][m];
            sc.nextLine();
            for (int x = 0; x < n; x++) {
                String chars = sc.nextLine();
                for (int y = 0; y < chars.length(); y++) {
                    maze[x][y] = chars.charAt(y) - '0';
                }
            }
            int id = 2;   //岛屿编号，从2开始
            int res = 0;
            for (int x = 0; x < n; x++) {
                for (int y = 0; y < m; y++) {
                    if (maze[x][y] == 1) {
                        if (!query(x, y)) {
                            res++;
                        }
                        //该位置是否为环
                        dfs(-1, -1, x, y, id++);
                    }
                }
            }
            System.out.println(res);
        }
    }

    static final int[][] dirs = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

    public static boolean dfs(int fx, int fy, int x, int y, int id) {
        if (maze[x][y] == id) {
            return ring[x][y] = true;
        }
        maze[x][y] = id;
        for (int[] dir : dirs) {
            int nx = dir[0] + x;
            int ny = dir[1] + y;
            if ((nx == fx && ny == fy) || nx < 0 || nx >= n || ny < 0 || ny >= m || (maze[nx][ny] != 1&&maze[nx][ny]!=id)) continue;
            if (dfs(x, y, nx, ny, id)) {
                ring[x][y] = true;
            }
        }
        return ring[x][y];
    }

    public static boolean query(int x, int y) {
        Set<Integer> pre;
        Set<Integer> cur = new HashSet<>();
        //up
        for (int i = 0; i < x; i++) {
            if (ring[i][y]){
                cur.add(maze[i][y]);    //遇到的环的id
            }
        }
        pre=cur;
        cur=new HashSet<>();
        //down
        for (int i = x+1; i < n; i++) {
            if (pre.contains(maze[i][y])){
                cur.add(maze[i][y]);
            }
        }
        //left
        pre=cur;
        cur=new HashSet<>();
        for (int i = 0; i < y; i++) {
            if (pre.contains(maze[x][i])){
                cur.add(maze[x][i]);
            }
        }
        pre=cur;
        cur=new HashSet<>();
        for (int i = y+1; i < m; i++) {
            if (pre.contains(maze[x][i])){
                cur.add(maze[x][i]);
            }
        }
        return cur.size()!=0;
    }

    static class Reader {
        private PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        private StringTokenizer tokenizer = new StringTokenizer("");

        private String innerNextLine() {
            try {
                return reader.readLine();
            } catch (IOException ex) {
                return null;
            }
        }

        public boolean hasNext() {
            while (!tokenizer.hasMoreTokens()) {    //内存中没有
                String nextLine = innerNextLine();
                if (nextLine == null) {     //且没有下一行
                    return false;
                }
                tokenizer = new StringTokenizer(nextLine);  //读入一行
            }
            return true;
        }

        public String nextLine() {
            tokenizer = new StringTokenizer("");    //清空内存中的字符
            return innerNextLine();
        }

        public void clear() {
            tokenizer = new StringTokenizer("");
        }

        public String next() {
            hasNext();
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        //    public BigInteger nextBigInt() {
//        return new BigInteger(next());
//    }

    }

}
